package me.hl.proxy;

import me.hl.normal.NormalTest;
import me.hl.normal.NormalTestBean;
import org.springframework.stereotype.Component;

import java.lang.reflect.Proxy;

/**
 * Created by Planck on 2016/1/28.
 */
public class SleepProxy {

    private final NormalTest normalTest;

    public SleepProxy(NormalTest normalTest){
        this.normalTest = normalTest;
    }


    public NormalTest proxy(){
        return (NormalTest) Proxy.newProxyInstance(
                NormalTest.class.getClassLoader(),
                new Class[]{NormalTest.class},
                (proxy,method,args)->{
                    Object ret = null;
                    if(method.getName().equals("sleep")){
                        System.out.println("proxy before sleep");
                        ret = method.invoke(normalTest,args);
                        System.out.println("proxy after sleep");
                    }else{
                        ret = method.invoke(normalTest,args);
                    }
                    return ret;
                }
        );
    }
}
